package com.smh.demo1.a10.jdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.UndeclaredThrowableException;

/**
 * @author shiminghui
 * @date 2025/4/12 17:48
 * @description: TODO
 */
//这个类并不存在,是在运行时动态生成的
public class $Proxy1 implements JDKProxyDemo.Inter {

    private static final Method test;
    private static final Method test2;

    static {
        try {
            test = JDKProxyDemo.Inter.class.getDeclaredMethod("test");
            test2 = JDKProxyDemo.Inter.class.getDeclaredMethod("test2");
        } catch (NoSuchMethodException e) {
            throw new Error(e);
        }
    }

    private final InvocationHandler invocationHandler;

    public $Proxy1(InvocationHandler invocationHandler) {
        this.invocationHandler = invocationHandler;
    }

    @Override
    public void test() {
        try {
            Object invoke = invocationHandler.invoke(this, test, new Object[0]);
        } catch (RuntimeException | Error e) {
            throw e;
        } catch (Throwable e) {
            throw new UndeclaredThrowableException(e);
        }
    }

    @Override
    public String test2() {
        try {
            Object invoke = invocationHandler.invoke(this, test2, new Object[0]);
            return (String) invoke;
        } catch (RuntimeException | Error e) {
            throw e;
        } catch (Throwable e) {
            throw new UndeclaredThrowableException(e);
        }
    }
}
